<?php
define('ROOT', dirname(__FILE__) . '/');
require_once ROOT . '/common.php';

ujn::runApplication( array(
	'templates_path'	=>  ROOT . '/views/',
	'models_path'		=>  ROOT . '/models/',
	'tmp_path'			=> ROOT . '/_tmp/',
	'dhs'				=> $dhs
) , 'alpha' == PLATFORM ? '__alpha_account_select' : '__account_save_userinfo' );

############################################

/**
 * 存储用户资料并进入游戏页
 * @param ujn_controller $c
 */
function __account_save_userinfo(ujn_controller $c) {
	$is_deb = false;

	$_o_platform = ujn_runtime::getParam(OBJ_PLATFORM);
	$_o_platform->init($_REQUEST['t']);

	$puid	= $_o_platform->getUserPlatformId();
	$uid	= mdl_account::getInstance()->getAccountUIDBySUID($puid);
	if( $uid > 0 && $uid < 1000 && ( isset($_REQUEST['deb']) && '1' == $_REQUEST['deb'] )) {
		$is_deb = true;
		if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && 0 != (int)$_REQUEST['uid']) {
			$uid = $_REQUEST['uid'];
		}
	}

	$is_deb && ujn_runtime::setParam(IS_DEBUG, true);

//	!$is_deb && die('服務器臨時維護, 請您稍後再來.');

	if(!$uid) {
		$u = $_o_platform->getUserInfo($_REQUEST['t']);
		$uid = mdl_account::getInstance()->makeAccount($u);
		if($uid) {
			$userinfo = $u->toArray();
			// 不存在并创建用户成功
			$userinfo['uid']			= $uid;
			$userinfo['is_register']	= '1'; // 第一次注册
		} else {
			die('安裝用戶失敗!');
		}
	} else {
		$is_needupdate = $_o_platform->isNeedUpdate($uid);
		if($is_needupdate) {
			mdl_account::getInstance()->updateAccount($uid, $_o_platform->getUserInfo());
		}

		// 非首次登陆流程
		$userinfo = mdl_field::getInstance()->getUserInfoByUID($uid);
		$userinfo['puid'] = $puid;
		$userinfo['is_needupdate'] = (int)$is_needupdate;
	}

	$friends = $_o_platform->getUserContact()->toArray();

	$game_url = $is_deb ? ( isset($_REQUEST['url']) ? urldecode($_REQUEST['url']) : GAME_URL ) : GAME_URL;

	$url 			= urlencode($game_url);
	$userinfo_json	= urlencode(json_encode($userinfo));
	$friends_json	= urlencode(json_encode($friends));

	$arr = array(
		'uid'				=> $uid,
		'url'				=> $url,
		'user'				=> $userinfo_json,
		'friends'			=> $friends_json,
		'time'				=> NOW,
		'sig'				=> md5($url . $uid . NOW . PRIVATE_KEY . $friends_json . $userinfo_json),

		'platform_sesskey'	=> $_o_platform->getSession(),
		'game_url'			=> $_o_platform->getGameUrl(),
	);

	$post_datas = array_merge($_REQUEST, $arr);

	if($is_deb && isset($_REQUEST['xdump'])) {
		echo '<pre>';
		var_dump($post_datas);
		echo '</pre>';
	} else if(method_exists($_o_platform, 'goGame')) {
		$_o_platform->goGame($game_url, $post_datas);
	}else {
		$c->assign(array(
			'post_datas' => $post_datas,
			'post_url' => $game_url
		));
		return $c;
	}
}

function __alpha_account_select(ujn_controller $c) {	
	$t = isset($_REQUEST['t']) ? (int)$_REQUEST['t'] : 0;
	switch ($t) {
		case 0:
			$datas = mdl_field::getInstance()->getAllUser();
			$c->assign('users', $datas);
			return $c;
			break;
		case 1: // 老用户登录
		case 2: // 新用户注册
			$c->setViewPath('__account_save_userinfo.php');
			return __account_save_userinfo($c);
			break;
	}
}
